package org.afun.csms_android.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.afun.csms_android.BasicMsgActivity;
import org.afun.csms_android.CameraActivity;
import org.afun.csms_android.MusicPlayer;
import org.afun.csms_android.R;
import org.afun.csms_android.VideoPlayer;
import org.afun.csms_android.adapter.UserItemAdapter;
import org.afun.csms_android.domain.User;
import org.afun.csms_android.domain.javaBean.ItemBean;
import org.afun.csms_android.tools.Music;
//import org.afun.csms_android.tools.Music;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ManagerMenu extends Fragment implements View.OnClickListener {

    private static Context context;
    private User user;
    private ListView listView;
    private List<ItemBean> data = new ArrayList<>();
    private TextView nickNameText;
    private TextView accountText;
    private static int state = 1;

    public ManagerMenu(Context context, User user) {
        ManagerMenu.context = context;
        this.user = user;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        data.add(new ItemBean("aFun的音乐播放器"));
        data.add(new ItemBean("aFun的视频播放器"));
        data.add(new ItemBean("aFun的相机"));
        data.add(new ItemBean("aFun的录像机"));
        data.add(new ItemBean("暂停/播放背景音乐"));
        data.add(new ItemBean("更多"));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_manager_menu, container, false);
        listView = view.findViewById(R.id.lv);
        listView.setAdapter(new UserItemAdapter(data, context));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Log.e("test", "onItemClick: " + position);
                Intent intent;
                switch (position) {
                    case 0:
                        Intent intentMusicPlayer = new Intent(context, MusicPlayer.class);
                        startActivity(intentMusicPlayer);
                        break;
                    case 1:
                        Intent intentVideoPlayer = new Intent(context, VideoPlayer.class);
                        startActivity(intentVideoPlayer);
                        break;
                    case 2:
                        Intent intentCamera = new Intent(context, CameraActivity.class);
                        startActivity(intentCamera);
                        break;
                    case 3:
                        Toast.makeText(context, "录像机！", Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        if (state == 2) {
                            // 正在播放就暂停
                            Intent intent1 = new Intent(context, Music.class);
                            intent1.putExtra("action", 1);
                            state = 1;
                            context.startService(intent1);
                        } else if (state == 1) {
                            // 暂停了就开始播放
                            Intent intent1 = new Intent(context, Music.class);
                            state = 2;
                            intent1.putExtra("action", 2);
                            context.startService(intent1);
                        }
                        break;
                    case 5:
                        Toast.makeText(context, "更多！", Toast.LENGTH_SHORT).show();
                        break;

                    default:
                        break;
                }
            }
        });


        nickNameText = view.findViewById(R.id.nickNameView);
        accountText = view.findViewById(R.id.textView4);

        nickNameText.setText(this.user.getName());
        accountText.setText(this.user.getAccount());

        // 设置点击基本信息后进入基本信息界面
        TextView basic_msg = view.findViewById(R.id.textView3);
        basic_msg.setOnClickListener(this);


        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textView3:
                Intent intent = new Intent(context, BasicMsgActivity.class);
                // 传递user
                Bundle bundle = new Bundle();
                bundle.putSerializable("user", user);
                intent.putExtras(bundle);
                context.startActivity(intent);
                break;
            default:
                break;
        }
    }

}